package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.StrategyTag;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.StaticSearchQuery;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.vo.StaticsVO;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyTagService strategyTagService;
    @Reference
    private IStrategyCommentService strategyCommentService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @RequestMapping("")
    public String index(Model model) {
        List<StrategyCommend> top5 = strategyCommendService.getTop5();
        model.addAttribute("commends", top5);
        //commends
        //unabroadCds abroadCds hotCds
        List<StrategyStatisVO> hotCds = strategyStatisVOService.queryHotCommends();
        model.addAttribute("hotCds", hotCds);
        List<StrategyStatisVO> abroadCds = strategyStatisVOService.queryAbroadCommends();
        model.addAttribute("abroadCds", abroadCds);
        List<StrategyStatisVO> unabroadCds = strategyStatisVOService.queryUnabroadCommends();
        model.addAttribute("unabroadCds", unabroadCds);
        //主题推荐前十 themeCds
        List<Map<String,Object>> themeCds=strategyTemplateSearchService.queryThemeCommends();
        model.addAttribute("themeCds", themeCds);

        //国内攻略推荐,chinas
        List<StaticsVO> chinas=strategyTemplateSearchService.queryConditionProvince();
        model.addAttribute("chinas", chinas);
        //国外攻略推荐,abroads
        List<StaticsVO> abroads=strategyTemplateSearchService.queryConditionCountries();
        model.addAttribute("abroads", abroads);
        //主题攻略推荐,themes
        List<StaticsVO> themes=strategyTemplateSearchService.queryConditionThemes();
        model.addAttribute("themes", themes);
        return "strategy/index";
    }

    @RequestMapping("/detail")
    public String detail(Model model, Long id, @UserParam UserInfo userInfo) {
        //detail
        StrategyDetail detail = strategyDetailService.get(id);
        detail.setStrategyContent(strategyDetailService.getContent(id));
        model.addAttribute("detail", detail);
        //需求当用户请求查看攻略明细时,让该攻略明细对应统计数viewnum+1
        strategyStatisVOService.increamViewnum(id, 1);
        //根据攻略明细id查处vo对象
        StrategyStatisVO vo = strategyStatisVOService.getStrategyStatisVO(id);
        model.addAttribute("vo", vo);
        if (userInfo != null) {
            //根据攻略明细id查当前攻略是否被当前用户收藏,如果收藏了,那么也页面显示效果
            Boolean ret = strategyStatisVOService.isFavor(id,userInfo.getId());
            model.addAttribute("isFavor", ret);
        }
        //热门攻略排序加分
        strategyStatisVOService.addHotScore(id, 1);
        return "strategy/detail";
    }
    //首页的高级查询
    @RequestMapping("/searchPage")
    public String searchPage(Model model, @ModelAttribute("qo")StaticSearchQuery qo){
        Page page=strategyTemplateSearchService.query(qo);
        model.addAttribute("page",page);
        return "strategy/searchPageTpl";
    }

    //攻略明细下的评论操作
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        strategyComment.setUserId(userInfo.getId());
        strategyComment.setLevel(userInfo.getLevel());
        //strategyComment.setCreateTime(new Date());在保存的时候设置
        strategyComment.setHeadUrl(userInfo.getHeadImgUrl());
        strategyComment.setCity(userInfo.getCity());
        strategyComment.setUsername(userInfo.getNickname());
        strategyCommentService.save(strategyComment);
        //攻略明细下的评论数+1
        strategyStatisVOService.increamReplynum(strategyComment.getDetailId(), 1);
        //先查询查vo对象,然后拿到vo对象的评论数,共享回页面
        int replynum = strategyStatisVOService.getStrategyStatisVO(strategyComment.getDetailId()).getReplynum();
        //热门攻略排序加分
        strategyStatisVOService.addHotScore(strategyComment.getDetailId(), 1);
        return AjaxResult.success().addData(replynum);
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo) {
        //根据传入的id,一致递归调用找他爸爸,放到数组当中,然后倒序遍历出来
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //删广州,返回值是单的所以直接返回共享过去就行了
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", dest);
        //ToastUtil.toast(model,qo.getDestId());
        //tags
        List<StrategyTag> tags = strategyTagService.list();
        model.addAttribute("tags", tags);
        //pageInfo
        PageInfo pageInfo = strategyDetailService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "strategy/list";
    }


    //攻略评论的列表
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo) {
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page", page);
        return "strategy/commentTpl";
    }

    //攻略评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid, Long fromid) {
        strategyCommentService.commentThumbUp(toid, fromid);
        return AjaxResult.success();
    }
    //攻略明细分享
    @RequestMapping("/share")
    @ResponseBody
    public Object share(Long sid,@UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            return result;
        }
        strategyStatisVOService.increamSharenum(sid,1);
        //分享成功之后给页面动态的获取分享数
        return new AjaxResult(true, "").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }

    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.NO_LOGIN);//并且状态码是102//因为是三个条件所以通过状态码来判断到底是哪个false
            return result;
        }
        //true表示用户收藏  false表示用户取消收藏
        Boolean ret = strategyStatisVOService.favor(sid, userInfo.getId());
        strategyStatisVOService.addAboardOrChinaScore(sid, ret ? 1 : -1);
        return new AjaxResult(ret, "").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }

    //攻略点赞
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid, @UserParam UserInfo userInfo) {
        if (userInfo == null) {
            AjaxResult result = new AjaxResult(false, "请先登录");
            result.setCode(AjaxResult.NO_LOGIN);//并且状态码是102//因为是三个条件所以通过状态码来判断到底是哪个false
            return result;
        }
        //true表示用户点赞  false表示用户已经顶过了
        Boolean ret = strategyStatisVOService.strategyThumbup(sid, userInfo.getId());
        if (ret) {
            strategyStatisVOService.addAboardOrChinaScore(sid, 1);
        }
        return new AjaxResult(ret, "").addData(strategyStatisVOService.getStrategyStatisVO(sid));
    }
}
